package com.dayatang.android.map;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * 绘制路径的图层
 * @author Administrator
 * 
 */
public class PolyLineLayer extends Overlay {
	
	private final List<GeoPoint> list;

	private final Paint paint = new Paint();

	public PolyLineLayer(List<GeoPoint> list) {
		this.list = list;
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(Color.BLUE);
		paint.setStrokeWidth(5);
	}

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {

		Path p = buildPath(mapView);
		canvas.drawPath(p, paint);

		return false;
	}

	private Path buildPath(MapView mapView) {
		Path p = new Path();
		for (int i = 0; i < list.size(); i++) {
			Point point = new Point();
			mapView.getProjection().toPixels(list.get(i), point);
			if (i == 0) {
				p.moveTo(point.x, point.y);
			} else {
				p.lineTo(point.x, point.y);
			}
			Log.v(PolyLineLayer.class.getSimpleName(), "x = " + point.x
					+ "y = " + point.y);
		}
		return p;
	}

}
